/**
 * 
 */
package com.zznode.client.wlanpolling.other;

import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zznode.client.wlanpolling.R;
import com.zznode.client.wlanpolling.model.NearbyApInfo;
import com.zznode.client.wlanpolling.model.NeighbouringAPManager;

/**
 * @author Pink
 * 
 */
public class NeighbouringAPListActivity extends ListActivity {
	private LinearLayout noApList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.neighbouring_aplist);

		noApList = (LinearLayout) findViewById(R.id.noApList);
		noApList.setVisibility(View.GONE);
		
		String tag = (String)getIntent().getExtras().get("tag");
		
		if(NeighbouringAPListTabActivity.TAB_1.equals(tag)) {
			List<NearbyApInfo> apinfos = NeighbouringAPManager.getNeighbouringAPManager().getNearbyApInfos1();
			if(apinfos.size() > 0) {
				setListAdapter(new NeighbouringHotListAdaptor(this, apinfos));
			} else {
				noApList.setVisibility(View.VISIBLE);
			}
		} else if(NeighbouringAPListTabActivity.TAB_5.equals(tag)) {
			List<NearbyApInfo> apinfos = NeighbouringAPManager.getNeighbouringAPManager().getNearbyApInfos5();
			if(apinfos.size() > 0) {
				setListAdapter(new NeighbouringHotListAdaptor(this, apinfos));
			} else {
				noApList.setVisibility(View.VISIBLE);
			}
		} else {
			List<NearbyApInfo> apinfos = NeighbouringAPManager.getNeighbouringAPManager().getNearbyApInfos10();
			if(apinfos.size() > 0) {
				setListAdapter(new NeighbouringHotListAdaptor(this, apinfos));
			} else {
				noApList.setVisibility(View.VISIBLE);
			}
		}
	}
}

class NeighbouringHotListAdaptor extends BaseAdapter {
	private List<NearbyApInfo> aplist;
	private Context context;

	public NeighbouringHotListAdaptor(Context context, List<NearbyApInfo> aplist) {
		this.aplist = aplist;
		this.context = context;
	}

	@Override
	public int getCount() {
		return aplist.size();
	}

	@Override
	public Object getItem(int position) {
		return aplist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;

		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.neighbouring_childitem, null);
		}

		NearbyApInfo apInfo = aplist.get(position);

		TextView txtAPName = (TextView) view.findViewById(R.id.txt_apname);
		txtAPName.setText(apInfo.NAME);

		TextView txtType = (TextView) view.findViewById(R.id.txt_type);
		txtType.setText(apInfo.TYPE);

		TextView txtAddress = (TextView) view.findViewById(R.id.txt_address);
		txtAddress.setText(apInfo.ADDRESS);

		TextView txtLon = (TextView) view.findViewById(R.id.txt_lon);
		txtLon.setText(apInfo.LON);

		TextView txtLat = (TextView) view.findViewById(R.id.txt_lat);
		txtLat.setText(apInfo.LAT);

		TextView txtDistance = (TextView) view.findViewById(R.id.txt_distance);
		txtDistance.setText(apInfo.DISTANCE + "m");

		return view;
	}
}